﻿using CommonFunction;
using MyWPF.Models.PC.Disk;
using MyWPF.StaticData;
using MyWPF.ViewModel.Subpage.MainPage_Son;
using MyWPF.Views.Others;
using Prism.Commands;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Drawing.Printing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using static MyWPF.ViewModel.Setting.VM_SetWindow;

namespace MyWPF.ViewModel.RightClick
{
    public class VM_PCState : BaseViewModel
    {
        public VM_PCState() 
        {
            #region 事件委托
            _close = new DelegateCommand<object>(_Close);
            _min = new DelegateCommand<object>(_Min);
            _moveWindow = new DelegateCommand<object>(_MoveWindow);
            _loadedWindow = new DelegateCommand<object>(_LoadedWindow);
            #endregion
            RefreshPC();
        }
        #region 基础：前端事件
        /// <summary>
        /// 关闭窗口
        /// </summary>
        private ICommand _close;
        public ICommand Close
        {
            get { return _close; }
            set
            {
                _close = value;
                OnPropertyChanged("Close");
            }
        }
        private void _Close(object obj)
        {
            StaticModels.pCState.Close();
        }
        /// <summary>
        /// 最小化窗口
        /// </summary>
        private ICommand _min;
        public ICommand Min
        {
            get { return _min; }
            set
            {
                _min = value;
                OnPropertyChanged("Min");
            }
        }
        private void _Min(object obj)
        {
            StaticModels.pCState.WindowState = System.Windows.WindowState.Minimized;
        }
        /// <summary>
        /// 移动窗口
        /// </summary>
        private ICommand _moveWindow;
        public ICommand MoveWindow
        {
            get { return _moveWindow; }
            set
            {
                _moveWindow = value;
                OnPropertyChanged("MoveWindow");
            }
        }
        private void _MoveWindow(object obj)
        {
            StaticModels.pCState.DragMove();
        }
        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChanged(string v)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(v));
            //throw new NotImplementedException();
        }
        #endregion


        #region 属性绑定：CPU内存信息等
        /// <summary>
        /// CPU型号
        /// </summary>
        public string CPUModel
        {
            get { return _cPUModel; }
            set { SetProperty(ref _cPUModel, value); }
        }
        private string _cPUModel;
        /// <summary>
        /// CPU型号
        /// </summary>
        public string GPUModel
        {
            get { return _gPUModel; }
            set { SetProperty(ref _gPUModel, value); }
        }
        private string _gPUModel;
        /// <summary>
        /// 内存使用百分比，保留1位小数
        /// </summary>
        public double MemoryPercentage
        {
            get { return _memoryPercentage; }
            set { SetProperty(ref _memoryPercentage, value); }
        }
        private double _memoryPercentage;
        /// <summary>
        /// 内存用量
        /// </summary>
        public string MemoryUse
        {
            get { return _memoryUse; }
            set { SetProperty(ref _memoryUse, value); }
        }
        private string _memoryUse;
        /// <summary>
        /// 内存总量
        /// </summary>
        public string TotalMemory
        {
            get { return _totalMemory; }
            set { SetProperty(ref _totalMemory, value); }
        }
        private string _totalMemory;
        #endregion

        #region 属性绑定：硬盘信息
        private UserControl _diskContent;
        public UserControl DiskContent
        {
            get { return _diskContent; }
            set { SetProperty(ref _diskContent, value); }
        }
        #endregion

        #region 事件绑定：界面Load事件
        /// <summary>
        /// 切换到常规
        /// </summary>
        private ICommand _loadedWindow;
        public ICommand LoadedWindow
        {
            get { return _loadedWindow; }
            set
            {
                _loadedWindow = value;
                OnPropertyChanged("LoadedWindow");
            }
        }
        private void _LoadedWindow(object obj)
        {
            DiskContent = new UserControl();

            DiskHelper diskHelper = new DiskHelper();
            StackPanel stack = new StackPanel();
            DiskContext disk = diskHelper.GetDiskInfo();
            for (int i = 0; i < disk.Name.Count; i++)
            {
                StackPanel stack_son = new StackPanel()
                {
                    Orientation= Orientation.Horizontal
                };
                TextBlock text1 = new TextBlock()
                {
                    Text = disk.Name[i] + "盘：",
                    Width= 60,
                    VerticalAlignment = System.Windows.VerticalAlignment.Center
                };
                ProgressBar progressBar = new ProgressBar()
                {
                    Width = 150, 
                    Maximum = 100, 
                    Minimum = 0, 
                    Value = disk.VolumeUsage[i], 
                    VerticalAlignment = System.Windows.VerticalAlignment.Center
                };
                TextBlock text2 = new TextBlock()
                {
                    Text = disk.UsedVolume[i] + "G",
                    FontSize = 16,
                    VerticalAlignment = System.Windows.VerticalAlignment.Center
                };
                TextBlock text3 = new TextBlock()
                {
                    Text = "/",
                    FontSize = 16,
                    VerticalAlignment = System.Windows.VerticalAlignment.Center
                };
                TextBlock text4 = new TextBlock()
                {
                    Text = disk.Volume[i] + "G",
                    FontSize = 16,
                    VerticalAlignment = System.Windows.VerticalAlignment.Center
                };
                stack_son.Children.Add(text1);
                stack_son.Children.Add(progressBar);
                stack_son.Children.Add(text2);
                stack_son.Children.Add(text3);
                stack_son.Children.Add(text4);
                stack.Children.Add(stack_son);
            }
            DiskContent.Content = stack;

            
            
        }
        #endregion

        #region 事件：获取电脑属性
        async void RefreshPC()
        {
            PCHelper pCHelper = new PCHelper();
            CPUModel = pCHelper.GetCPUModel();
            GPUModel = pCHelper.GetGPUModel();
            while (true)
            {
                await Task.Delay(1000);
                await Task.Run(() =>
                {
                    pCHelper = new PCHelper();
                    //内存总量获取
                    double GetTotalMemorySize = pCHelper.GetTotalMemorySize();
                    //内存使用量获取
                    double GetCurrentMemoryUsage = pCHelper.GetCurrentMemoryUsage();
                    //通知给界面
                    TotalMemory = GetTotalMemorySize.ToString("f2") + "G";
                    MemoryUse = GetCurrentMemoryUsage.ToString("f2") + "G";
                    MemoryPercentage = Math.Round((GetCurrentMemoryUsage / GetTotalMemorySize) * 100, 2);
                });
            }
            
        }
        #endregion
    }
}
